package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwddT;
import cn.e4j.modular.crm.mapper.GwdcTMapper;
import cn.e4j.modular.crm.mapper.GwddTMapper;
import cn.e4j.modular.system.entity.SysOrg;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  合同协议维护 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-02-01
 */
@Service
public class Gwdc005ServiceImpl extends ServiceImpl<GwddTMapper, GwddT>{

    @Autowired
    GwdcTMapper gwdcTMapper;

    @Autowired
    public SysAuthorityService sysAuthorityService;

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwddlist(Page<Object> page, Dto dto) {
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
        //部门权限
        dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));
//        List<Map<String, Object>> gwddDtos = this.baseMapper.listGwddPage(page, dto);
        List<Map<String, Object>> gwddDtos = InvokeUtil.invokeBeanMethod("gwddTMapper,listGwddPage",page,dto);
        return gwddDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwddData(Dto dto) {
//        List<Map<String, Object>> gwddDtos = this.baseMapper.getGwddlist(dto);
        List<Map<String, Object>> gwddDtos = InvokeUtil.invokeBeanMethod("gwddTMapper,getGwddlist",dto);
        return gwddDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwdelist(Dto dto) {
        List<Map<String, Object>> gwdeDtos = this.baseMapper.getGwdelist(dto);
        return gwdeDtos;
    }

    @Transactional(readOnly = true)
    public Dto getAddData(Dto dto) {
        //当前登录者及部门
        ShiroUser shiroUser = ShiroKit.getUser();
        dto.put("GWDD007",shiroUser.getID());
        dto.put("GWDD007_NAME",shiroUser.getNAME());
        dto.put("GWDD008",shiroUser.getORG_ID());
        SysOrg sysOrg = this.getOrg(shiroUser.getORG_ID());
        dto.put("GWDD008_NAME",sysOrg.getNAME());

        Date date = new Date();
        dto.put("GWDD005", DateUtils.formatDate(date,DateUtils.PATTERN_yyyy_MM_dd));//生效日期
        dto.put("GWDD004",DateUtils.formatDate(date,DateUtils.PATTERN_yyyy_MM_dd));//单据日期
        dto.put("GWDD006",DateUtils.getYear()+"-12-31");//单据日期
        dto.put("GWDD001","FLXY" + DateUtils.formatDate(date, DateUtils.PATTERN_yyyyMMddHHmmss));
        return dto;
    }

    public SysOrg getOrg(String id){
        SysOrg sysOrg = this.gwdcTMapper.getOrgInfo(id);
        return sysOrg;
    }

    @Transactional
    public void addData(List<GwddT> gwddTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Date date = new Date();
        String GWDDTIME = DateUtils.formatDate(date, "HH:mm:ss");
        for(GwddT gwddT:gwddTList){
            gwddT.setGWDDENT(shiroUser.getDBC());
            gwddT.setGWDDACTI("N");
            gwddT.setGWDDTIME(GWDDTIME);
            gwddT.insert();
        }
    }

    @Transactional
    public void editData(List<GwddT> gwddTList,String gwdd001) {
        ShiroUser shiroUser = ShiroKit.getUser();

        GwddT gwddT_del = new GwddT();
        UpdateWrapper<GwddT> gwddTUpdateWrapper = new UpdateWrapper<>();
        gwddTUpdateWrapper.eq("GWDDENT", shiroUser.getDBC());
        gwddTUpdateWrapper.eq("GWDD001", gwdd001);
        gwddT_del.delete(gwddTUpdateWrapper);

        Date date = new Date();
        String GWDDTIME = DateUtils.formatDate(date, "HH:mm:ss");
        for(GwddT gwddT:gwddTList){
            gwddT.setGWDDENT(shiroUser.getDBC());
            gwddT.setGWDDACTI("N");
            gwddT.setGWDDTIME(GWDDTIME);
            gwddT.insert();
        }
    }

    @Transactional
    public void delete(Dto dto) {
        GwddT gwddT = new GwddT();
        UpdateWrapper<GwddT> gwddTUpdateWrapper = new UpdateWrapper<>();
        gwddTUpdateWrapper.eq("GWDDENT", dto.getString("GWDDENT"));
        gwddTUpdateWrapper.eq("GWDD001", dto.getString("GWDD001"));

        gwddT.delete(gwddTUpdateWrapper);
    }

    @Transactional
    public void delDataBatch(List<GwddT> gwddTList) {
        for (GwddT gwddT : gwddTList) {
            UpdateWrapper<GwddT> gwddTUpdateWrapper = new UpdateWrapper<>();
            gwddTUpdateWrapper.eq("GWDDENT", gwddT.getGWDDENT());
            gwddTUpdateWrapper.eq("GWDD001", gwddT.getGWDD001());

            gwddT.delete(gwddTUpdateWrapper);
        }
    }

    @Transactional
    public void updateData(List<GwddT> gwddTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        for (GwddT gwddT : gwddTList) {

            if(gwddT.getGWDDACTI().equals("Y")) {
                gwddT.setGWDD009(shiroUser.getID());
                gwddT.setGWDD010(DateUtils.formatDate(new Date(), DateUtils.PATTERN_yyyyMMdd));
            }else {
                gwddT.setGWDD009("");
                gwddT.setGWDD010("");
            }

            UpdateWrapper<GwddT> gwddTUpdateWrapper = new UpdateWrapper<>();
            gwddTUpdateWrapper.eq("GWDDENT", gwddT.getGWDDENT());
            gwddTUpdateWrapper.eq("GWDD001", gwddT.getGWDD001());
            gwddT.update(gwddTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwddocno(Page<Object> page, Dto dto) {
        List<Map<String, Object>> gwddDtos = this.baseMapper.getGwddocno(page, dto);
        return gwddDtos;
    }
}
